package com.example.HelloSpace.service;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.example.HelloSpace.R;
import com.example.HelloSpace.models.ContentElements;
import com.example.HelloSpace.models.ContentModel;
import com.google.android.material.imageview.ShapeableImageView;
import com.google.android.material.textview.MaterialTextView;

public class ContentViewGenerator {
    private final LinearLayout container;
    private final Context context;
    private final String url;
    public ContentViewGenerator(LinearLayout container, Context context) {
        this.container = container;
        this.context = context;
        this.url = context.getResources().getString(R.string.SERVER_URL);
    }
    public void createView(ContentModel contentModel) {
        for (ContentElements contentElements : contentModel.getContent()) {
            View view= viewCreator(contentElements);
            if(view!=null) {
                configureView(view, contentElements);
                container.addView(view);
            }
        }
    }

    private View viewCreator(ContentElements contentElements) {
        switch (contentElements.getType()) {
            case "h1":
            case "h2":
            case "p":
                return createTextView(contentElements);
            case "img":
                return createImageView(contentElements);
            case "video":
                return createVideoView(contentElements);
            default:
                return null;
        }
    }

    private View createVideoView(ContentElements contentElements) {
        return null;
    }

    private View createImageView(ContentElements contentElements) {
        Log.d("ImageView", "createImageView");
        ShapeableImageView imageView = new ShapeableImageView(context);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

        if (contentElements.getContent() != null && !contentElements.getContent().isEmpty()) {
            Glide.with(imageView.getContext())
                    .load(url+contentElements.getContent())
                    .into(imageView);
            Log.d("ImageView", url+contentElements.getContent());
        }
        imageView.setLayoutParams(new LinearLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT, // 宽度为 match_parent
                ConstraintLayout.LayoutParams.WRAP_CONTENT   // 高度为 wrap_content
        ));
        imageView.setPadding(20,20,20,20);
        return imageView; // 返回创建的 ImageView
    }

    private View createTextView(ContentElements contentElements) {
        MaterialTextView textView = new MaterialTextView(context);
        textView.setText(contentElements.getContent());
        textView.setTextColor(ContextCompat.getColor(context, R.color.black));
        switch(contentElements.getType()){
            case "h1":
                textView.setPadding(20,32,20,16);
                textView.setTypeface(Typeface.DEFAULT_BOLD);
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,24);
                break;
            case "h2":
                textView.setPadding(20,24,20,16);
                textView.setTypeface(Typeface.DEFAULT_BOLD);
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
                break;
            case "p":
                textView.setPadding(20,0,20,0);
                textView.setLineSpacing(24,1.0f);
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
                textView.setTextColor(Color.parseColor("#333333"));
                break;
        }
        return textView;
    }

    private void configureView(View view,ContentElements contentElements) {


    }
}
